<script setup>
import { ref, reactive, computed, onMounted } from 'vue';
import { warnApi } from '@T2/service/api.ts';
import { getUnitsParams } from '@T2/utils/params.ts';
import { useIuserInfoStore } from '@T2/store/users.ts';
import { buildTree } from '@T2/utils/utils.ts';
import Position1 from '@T2/views/Project/Display/components/treeSelecteds/position1.vue';
import ZutaiShow from '@T2/components/zutaiShow/index.vue';

const iuserInfoStore = useIuserInfoStore();
const { info, getHandleRight } = iuserInfoStore;
const RIGHT = getHandleRight(['394']);

const Resd = reactive({
  filterText: '全部',
});
const units = reactive({
  list: [],
  currentList: [],
});
onMounted(() => {
  getUnitsParams('/P/Manage/unit01').then(res => {
    const { unitList } = res;
    unitList.forEach(item => {
      item._show = true;
      item._sensors_num = [];
      item._sensors_states = {};
      if (item._sensors?.length) {
        const [ _sensors_states, _sensors_num ] = item._sensors;
        item._sensors_states = _sensors_states || {};
        item._sensors_num = _sensors_num?.c || [];
      }
    });
    units.list = unitList;
    units.currentList = units.list.filter(i => true);
  });
});

/* tab切换 */
const tabs = ref(1);
const tabsClick = (node) => {
  tabs.value = node;
  if (node === 1) {
    units.currentList.forEach(item => {
      item._show = true;
    });
  } else if (node === 2) {
    units.currentList.forEach(item => {
      item._show = !item.warn_number ? true : false;
    });
  } else if (node === 3) {
    units.currentList.forEach(item => {
      item._show = item.warn_number ? true : false;
    });
  } else if (node === 4) {
    units.currentList.forEach(item => {
      item._show = item.users.includes(info.id) ? true : false;
    });
  }
}

/* 数量统计 */
const sysNums = computed(() => {
  let alarm = 0, normal = 0, cares = 0;
  units.currentList.forEach(item => {
    item.warn_number && alarm++;
    !item.warn_number && normal++;
    item.users.includes(info.id) && cares++;
  });
  return {alarm, normal, cares};
});

/* 组态弹窗 */
const ztResd = reactive({
  show: false,
  page: null,
});
const ztIconClick = (item) => {
  if (item.params) {
    const paramsObj = JSON.parse(item.params);
    if (paramsObj?.others?.length) {
      let pages = paramsObj.others[0];
      if (pages?.c && pages.c[0].t === 'zt') {
        ztResd.show = true;
        ztResd.page = pages.c[0].v;
      }
    }
  }
}

const positionChange = (data, node) => {
  Resd.filterText = data.name;
  let ids = [];
  let children = data.children;
  if (node.level === 1) {
    ids = data.children.map(i => i.id);
    units.currentList = units.list.filter(i => ids.includes(i.positon));
  } else if (node.level === 2) {
    units.currentList = units.list.filter(i => i.positon === data.id);
  }
}
const positionClear = () => {
  Resd.filterText = '全部';
  units.currentList = unitList.filter(i => true);
}
</script>

<template>
  <div class="view flex-column">
    <div class="header flex-sb">
      <div class="tabs flex--center">
        <Position1 @current-change="positionChange" @clear="positionClear" />
        <div class="tab flex-center-center" :class="{'isActive': tabs === 1}" @click="tabsClick(1)">
          <span>{{ Resd.filterText }}（{{ units.currentList.length }}）</span>
        </div>
        <div class="tab flex-center-center" :class="{'isActive': tabs === 2}" @click="tabsClick(2)">
          <div class="circle" style="background-color: #75DD64;"></div>
          <span>正常（{{ sysNums.normal }}）</span>
        </div>
        <div class="tab flex-center-center" :class="{'isActive': tabs === 3}" @click="tabsClick(3)">
          <div class="circle" style="background-color: #D9001B;"></div>
          <span>报警（{{ sysNums.alarm }}）</span>
        </div>
        <div class="tab flex-center-center" :class="{'isActive': tabs === 4}" @click="tabsClick(4)">
          <img :src="require('@T2/assets/common/cares-yes.png')" />
          <span>关注（{{ sysNums.cares }}）</span>
        </div>
      </div>
      <div class="iconEG flex">
        <div class="example flex--center">
          <div class="green"></div>
          <!-- <img :src="require('@T2/assets/device/pump-normal.png')" /> -->
          <span>运行</span>
        </div>
        <div class="example flex--center">
          <div class="red"></div>
          <!-- <img :src="require('@T2/assets/device/pump-alarm.png')" /> -->
          <span>故障</span>
        </div>
        <div class="example flex--center">
          <div class="gray"></div>
          <!-- <img :src="require('@T2/assets/device/pump-stop.png')" /> -->
          <span>离线</span>
        </div>
      </div>
    </div>
    
    <div class="grid-box flex-center scroller-aside">
      <div class="grid-item pointer flex-column" v-for="unit in units.currentList" v-show="unit._show" @click="ztIconClick(unit)">
        
        <div class="info-state-box flex-column">
          <div class="unitName-state flex--center">
            <label>{{ unit.name }}</label>
            <img :src="unit.users.includes(info.id) ? require('@T2/assets/common/cares-yes.png') : require('@T2/assets/common/cares-no.png')" />
            <div class="status-box flex--center" v-if="unit._sensors_states?.c?.length">
              <template v-for="item in unit._sensors_states.c">
                <div :class="{'运行': 'green', '故障': 'red'}[item.value] || 'gray'"></div>
                <!-- <img :src="require('@T2/assets/device/pump-normal.png')" v-if="item.value === '运行'" /> -->
                <!-- <img :src="require('@T2/assets/device/pump-alarm.png')" v-else-if="item.value === '故障'" /> -->
                <!-- <img :src="require('@T2/assets/device/pump-stop.png')" v-else /> -->
              </template>
            </div>
          </div>
          <div class="addr-tips flex-sb-center">
            <span class="addr">{{ unit.addr }}</span>
            <span>{{ unit._sensors_states.n }}</span>
          </div>
          <span class="timestamp">{{ (unit._sensors_num || []).find(i => i.timestamp)?.timestamp }}</span>
        </div>
        
        <div class="datas-line flex--center flex-nowrap">
          <template v-if="unit._sensors_num?.length">
            <div class="data flex-column" v-for="item in unit._sensors_num">
              <span :style="{color: item._warn ? '#FE4C43' : '#4D92F5'}">{{ !item.value || item.value === '掉线' ? '- -' : item.value }}</span>
              
              <div class="data-name flex-center flex-column">
                <span>{{ item.n }}<template v-if="item.unit">({{ item.unit }})</template></span>
              </div>
            </div>
          </template>
        </div>
        <div class="alarmHr" v-if="unit.warn_number"></div>
      </div>
    </div>
  </div>
  
  <div class="ztBox" v-if="ztResd.show">
    <ZutaiShow :dftPage="ztResd.page" :disabled="!RIGHT['394']" />
    <div class="closezt pointer" @click="ztResd.show = false">
      <el-icon size="30" color="#bababa"><CircleClose /></el-icon>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.view {
  background-color: transparent;
  height: 100%;
  width: 100%;
}
.header {
  width: 100%;
  height: 65px;
  border-bottom: 1px solid rgba(255,255,255,0.3);
  box-shadow: 0px 4px 4px 0px rgba(0,0,0,0.25);
  padding: 0 52px;
  .tabs {
    height: 100%;
    .tab {
      min-width: 130px;
      height: 100%;
      padding: 0 10px;
      font-size: 18px;
      color: #D3F2EE;
      cursor: pointer;
      margin-right: 20px;
      white-space: nowrap;
      .circle {
        width: 12px;
        height: 12px;
        border-radius: 50%;
        margin-right: 6px;
      }
      img {
        width: 15px;
        height: 15px;
        margin-right: 6px;
      }
      &.isActive {
        font-weight: 500;
        color: #00CCCC;
        position: relative;
        ::after {
          content: ' ';
          height: 3px;
          width: 100%;
          background-color: #00CCCC;
          position: absolute;
          bottom: -2px;
          left: 0;
        }
      }
    }
  }
  .iconEG {
    margin-left: auto;
    color: rgba(211,242,238,0.6);
    padding: 4px;
    .example {
      margin-left: 30px;
      img {
        width: 17px;
        height: 17px;
        margin-right: 6px;
      }
    }
  }
}
.green, .red, .gray {
  width: 17px;
  height: 17px;
  margin-right: 5px;
  border-radius: 50%;
}
.green {
  background-color: #75DD64;
}
.red {
  background-color: #D9001B;
}
.gray {
  background-color: #8e8e8e;
}
.grid-box {
  flex: 1;
  padding: 35px 40px 10px;
  overflow-x: hidden;
  overflow-y: overlay;
  display: grid;
  gap: 24px;
  grid-template-columns: repeat(auto-fill, minmax(385px, 1fr));
}
.grid-item {
  min-width: 385px;
  max-width: 500px;
  height: 188px;
  background: #002B2D;
  overflow: hidden;
  position: relative;
  .alarmHr {
    position: absolute;
    background-color: #F30C0C;
    height: 2px;
    width: calc(100% - 10px);
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
  }
  &:hover {
    box-shadow: 0px 1px 5px 1px #025A5A;
    transform: translateY(-2px);
  }
}
.info-state-box {
  height: 104px;
  width: 100%;
  background-color: #002B2D;
  padding: 15px 10px 0 15px;
  font-weight: 400;
  .unitName-state {
    label {
      font-size: 20px;
      color: #D3F2EE;
      margin-right: 8px;
    }
    img {
      width: 19px;
      height: 19px;
    }
    .status-box {
      margin-left: auto;
    }
  }
  .addr-tips {
    margin-top: 5px;
    font-size: 12px;
    color: rgba(211,242,238,0.6);
    .addr {
      // color: #5290F7;
    }
  }
  .timestamp {
    margin-top: 4px;
    font-size: 12px;
    color: rgba(211,242,238,0.6);
  }
}
.datas-line {
  height: 84px;
  width: 100%;
  background-color: #00242E;
  position: relative;
  .data {
    width: 25%;
    padding: 0 10px;
    >span {
      font-size: 20px;
      line-height: 23px;
    }
    .data-name {
      width: fit-content;
      span {
        margin-top: 2px;
        width: fit-content;
        text-align: left;
        font-size: 12px;
        color: rgba(211,242,238,0.6);
        line-height: 14px;
        white-space: nowrap;
      }
    }
  }
}
.ztBox {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: #001C20;
  z-index: 2;
  .closezt {
    position: absolute;
    right: 10px;
    top: 10px;
  }
}
</style>